# -*- coding:utf-8 -*-
__author__ = 'wangjvv'
'''
date: 2021/03/09
description: 处理数据库依赖
'''
from config.setting import host,port,user,passwd

import pymysql

def handle_mysql(db):
    '''连接数据库获取游标'''
    conn = pymysql.connect(host=host,port=port,user=user,passwd=passwd,db=db)  #记得改用户名密码
    cur = conn.cursor()
    return cur

def handle_tuple(tuple):
    '''执行sql语句后的结果为特殊元祖型，将返回的元祖数据转换为列表 '''
    result = []
    for i in tuple:
        result.append(i[0])
    return result

def handle_sql(db,sql):
    '''执行查询sql语句，并获取指定的字段'''
    cur = handle_mysql(db) # 获取mysql连接游标
    cur.execute(sql)  #执行sql语句
    response = handle_tuple(cur.fetchall()) #获取查询结果并转换为列表
    return response




def getSubProjectId(*project_stage_name):
    '''根据分期名获取分期Id'''
    sql = 'select id from yd_cloud.park_project_sub where project_stage_name = "{}" and project_id = 130'.format(project_stage_name[0])
    subProjectId = handle_sql('yd_cloud',sql)[0]
    return subProjectId


def excute_function(function,*args,**kwargs):
    '''执行传入的方法'''
    value = function(*args,**kwargs)
    return value



if __name__ == '__main__':
    # getSubProjectId('测试分期')
    excute_function(getSubProjectId,'测试分期')